<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>导入</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <style type="text/css">
        .redstar{
            color: red;
            margin-right: 5px;
        }
        .layui-field-title{
            color: #ffffff;
        }

    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" lay-filter="modelInfo" action="" style="margin:10px 20px 0 10px">
            <input type="text" id="id" name="id" class="layui-input" style="display: none">
            <input type="text" id="pid" name="pid" class="layui-input" style="display: none">
            <button type="button" class="layui-btn" id="test3"><i class="layui-icon"></i>上传文件</button>
        </form>
    </div>
</div>


<script src="../layui/layui.js" charset="utf-8"></script>
<script src="../js/jquery-1.9.0.js"></script>
<script type="text/javascript" src="../js/common.js"></script>

<script>
    layui.use(['form','element','table','upload'],function () {
        var element = layui.element,
            form = layui.form,
            table = layui.table;
            upload = layui.upload;

        /**
         * 获取初始数据
         */
        var modelId = getUrlParams('modelId'),
            parentId = getUrlParams('pid'),
            code = getUrlParams('code'),
            modelName = getUrlParams('modelName'),
            remark = getUrlParams('remark');

        //拖拽上传
        // upload.render({
        //     elem: '#test10'
        //     ,url: '/baseDataManagerServer/modelTable/importExcel' //改成您自己的上传接口
        //     ,done: function(res){
        //         layer.msg('上传成功');
        //         layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.files.file);
        //         console.log(res)
        //     }
        // });
        upload.render({
            elem: '#test3'
            ,url: '/baseDataManagerServer/modelTable/importExcel' //改成您自己的上传接口
            ,accept: 'file' //普通文件
            ,before: function () {
                this.data = {
                    modelId: modelId,
                    modelName: modelName
                }
                layer.load(); //上传loading
            }
            ,done: function(res){
                if(res.code == '0000'){
                    layer.msg('上传成功');
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index);
                    parent.layui.table.reload('menuIndexTable',{});
                }else{
                    layer.msg('上传失败，原因:'+res.msg)
                }
                layer.closeAll('loading')
                //console.log(res);
            }
        });

        $(document).ready(function () {
            if (modelId !== null && modelId !== undefined) {
                form.val('modelInfo', {
                    'id': modelId,
                    'pid':parentId,
                    'code': code,
                    'name': name,
                    // 'remark': remark
                });
                if(remark!=='null'){
                    form.val('modelInfo',{
                        'remark':remark
                    });
                }
            }else{
                form.val('modelInfo',{
                    'pid':parentId
                });
            }
        });


        window.addEventListener('message', function (event) {
            if (event.data === 'submit') {
                $("#yes").trigger("click");
            }
        });


        /**
         * 监听提交
         */
        form.on('submit(submit)', function (data) {
            if (data.field.id != null && data.field.id !== '') {

                /**
                 * 修改目录信息
                 */
                $.ajax({
                    type: "post",
                    url: "/baseDataManagerServer/model/updateModel",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('update_success-' + redata.msg, '*');
                        } else if (redata.code === '9999') {
                            window.parent.postMessage('update_error-' + redata.msg, '*');
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('update_error', '*');
                    }
                });
            } else {
                $.ajax({
                    type: "post",
                    url: "/baseDataManagerServer/model/insertModel",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('success-' + redata.msg, '*');
                        } else if (redata.code === '9999') {
                            window.parent.postMessage('error-' + redata.msg, '*');
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('error', '*');
                    }
                });
            }
            return false;
        });
    });

    window.getUrlParams = function (key) {
        //获取url参数
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
        var url = decodeURI(window.location.search)
        var r = url.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
</script>

</body>
</html>